package com.alibaba.otter.canal.example.mysql;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author heweiye
 * @date 2021/3/16
 * @description: TODO
 */
public class DbCustomerHelper {

    private static AtomicInteger count=new AtomicInteger();



    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException, InterruptedException {
        String connectStr = "jdbc:mysql://hwy04:3306/canal-hwy";
        String insert_sql = "INSERT INTO tb_order (`create_time`, `status`) VALUES (?,?);";
        String username = "root";
        String password = "123456";

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(connectStr, username,password);
        conn.setAutoCommit(false); // 设置手动提交

        List<Thread> list=new ArrayList<>();

        for (int i = 0; i <3 ; i++) {
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (count.get()<=2000000){
                            PreparedStatement psts = conn.prepareStatement(insert_sql);
                            for (int i = 0; i < 500; i++) {

//                                psts.setString(2, EmailRandom.getEmail(1,10));
                                psts.setDate(1,new Date(System.currentTimeMillis()) );
//                                psts.setLong(4,System.currentTimeMillis() );
                                psts.setInt(2, new Random().nextInt(10));
                                psts.addBatch();
                            }

                            psts.executeBatch(); // 执行批量处理
                            conn.commit();  // 提交
                            count.getAndAdd(500);
                            System.out.println(Thread.currentThread().getName()+":已插入记录条数："+count.get());
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            },"线程-"+i);
            thread.start();
            list.add(thread);
        }
        for (Thread thread : list) {
            thread.join();
        }
        conn.close();
        System.out.println("end-----------------");
    }




    public static void channel(String[] args) throws SQLException, IOException, ClassNotFoundException, InterruptedException {
        String connectStr = "jdbc:mysql://hwy04:3306/canal-kafka";
        String insert_sql = "INSERT INTO channel (`customer_id`, `channel_name`) VALUES (?,?);";
        String username = "root";
        String password = "123456";

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(connectStr, username,password);
        conn.setAutoCommit(false); // 设置手动提交

        List<Thread> list=new ArrayList<>();
        Random random=new Random();


        for (int i = 0; i <3 ; i++) {
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (count.get()<=10){
                            PreparedStatement psts = conn.prepareStatement(insert_sql);
                            for (int i = 0; i < 5; i++) {
                                psts.setInt(1, random.nextInt(10));
                                psts.setString(2, "渠道"+UUID.randomUUID().toString().substring(0,5));
                                psts.addBatch();
                            }

                            psts.executeBatch(); // 执行批量处理
                            conn.commit();  // 提交
                            count.getAndAdd(500);
                            System.out.println(Thread.currentThread().getName()+":已插入记录条数："+count.get());
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            },"线程-"+i);
            thread.start();
            list.add(thread);
        }
        for (Thread thread : list) {
            thread.join();
        }
        conn.close();
        System.out.println("end-----------------");
    }



}
